﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BlogPlat.Model.Abstract;
using BlogPlat.Model;
using System.Text.RegularExpressions;
using BlogPlat.Model.Repositories;

namespace BlogPlat.Areas.Admin.Controllers
{
    [Authorize(Roles = "admin,author")]
    public class TagController : Controller
    {
        private IRepository<Tag> _repository;

        public TagController(IRepository<Tag> repository) 
        {
            _repository = repository;
        }

        //
        // GET: /Admin/Tag/

        public ActionResult Index()
        {
            return View(_repository.List());
        }        

        //
        // GET: /Admin/Tag/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Admin/Tag/Create

        [HttpPost]
        public ActionResult Create(Tag newTag)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    newTag.FriendlyUrl = newTag.FriendlyUrl.Replace(" ", "-");
                    newTag.FriendlyUrl = Regex.Replace(newTag.FriendlyUrl, "[\"$&+,/:;=?@]", String.Empty);

                    _repository.Add(newTag);
                    _repository.Save();
                    return RedirectToAction("Index");
                }

                throw new Exception();
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Admin/Tag/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View(_repository.Get(id));
        }

        //
        // POST: /Admin/Tag/Edit/5

        [HttpPost]
        public ActionResult Edit(Tag tagToUpdate)
        {
            if(ModelState.IsValid)
            {                                   
                tagToUpdate.FriendlyUrl = tagToUpdate.FriendlyUrl.Replace(" ", "-");
                tagToUpdate.FriendlyUrl = Regex.Replace(tagToUpdate.FriendlyUrl, "[\"$&+,/:;=?@]", String.Empty).ToLower();

                _repository.Update(tagToUpdate);
                _repository.Save();

                return RedirectToAction("Index");
            }

            return View();
        }        

        //
        // POST: /Admin/Tag/Delete/5

        [HttpPost]
        public ActionResult Delete(int id)
        {
            try
            {                
                _repository.Delete(id);
                _repository.Save();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult DeleteButton(Tag tag)
        {
            ViewData["PostCount"] = CountPostsTagged(tag.TagID);
            return PartialView("DeleteButton", tag);
        }

        [ChildActionOnly]
        public int CountPosts(int id)
        {
            return CountPostsTagged(id);
        }

        private int CountPostsTagged(int id)
        {
            return ((TagRepository)_repository).CountPostsTagged(id);
        }
    }
}
